package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Business;
import cn.wolfcode.enums.ConsumptionEnum;
import cn.wolfcode.enums.ReportEnum;
import cn.wolfcode.qo.ReportQueryObject;
import cn.wolfcode.service.IBusinessReportService;
import cn.wolfcode.service.IBusinessService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Author wyy
 * Date 2021/1/17 16:41
 **/
@Controller
@RequestMapping("/businessReport")
public class BusinessReportController {
    @Autowired
    private IBusinessReportService businessReportService;
    @Autowired
    private IBusinessService businessService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ReportQueryObject qo){
        List<Business> businesses = businessService.listAll();
        model.addAttribute("businesses",businesses);
        //结算状态
        ConsumptionEnum[] cStatus = ConsumptionEnum.values();
        model.addAttribute("cStatus",cStatus);
        //分组条件
        ReportEnum[] groupTypes = ReportEnum.values();
        model.addAttribute("groupTypes",groupTypes);
        //列表
        PageInfo<HashMap> pageInfo = businessReportService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        //准备图表数据
//        List groupList = new ArrayList();
//        pageInfo.setPageSize(0);
//        for ()

        return "/businessReport/list";
    }

    @RequestMapping("/listBar")
    public String listBar(Model model, @ModelAttribute("qo") ReportQueryObject qo){
        qo.setPageSize(0);
        List<HashMap> list = businessReportService.query(qo).getList();

        //准备图表数据
        ArrayList<Object> glist = new ArrayList<>();//分组类型
        ArrayList<Object> totalCountlist = new ArrayList<>();//订单数
        ArrayList<Object> totalAmountlist = new ArrayList<>();//总消费金额
        ArrayList<Object> payAmountlist = new ArrayList<>();//总实收金额
        ArrayList<Object> discountAmountlist = new ArrayList<>();//总优惠金额

        for (HashMap map : list) {
            glist.add(map.get("groupType"));
            totalCountlist.add(map.get("totalCount"));
            totalAmountlist.add(map.get("totalAmount"));
            payAmountlist.add(map.get("payAmount"));
            discountAmountlist.add(map.get("discountAmount"));
        }
        model.addAttribute("glist", JSON.toJSONString(glist));
        model.addAttribute("totalCountlist",JSON.toJSONString(totalCountlist));
        model.addAttribute("totalAmountlist",JSON.toJSONString(totalAmountlist));
        model.addAttribute("payAmountlist",JSON.toJSONString(payAmountlist));
        model.addAttribute("discountAmountlist",JSON.toJSONString(discountAmountlist));

        return "/businessReport/listBar";
    }

}
